package servlet.order;

import entity.*;
import service.*;
import service.impl.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @create 2021-04-11-14:58
 */
@WebServlet("/OrderSelect")
public class OrderSelect extends HttpServlet {

    private CartService cartService = new CartServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cartIds = request.getParameter("cart_id");
        System.out.println(cartIds); //1,2
        int totalAmount = 0;//订单总金额
        int productCount = 0;
        //按照,进行切分
        String[] cartIdList = cartIds.split(",");
        List<Order> orderList = new ArrayList<>();
        for (String cartId : cartIdList) {
            System.out.println(cartId);
            //查询出每一个cardId的信息
            Cart cart = cartService.findCartListByCartId(Integer.valueOf(cartId));
            System.out.println(cart);

            //保存每一条订单
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("name");
            CategoryService service1 = new CategoryServiceImpl();
            List<Category> flist = service1.findCategoryListByName("father");
            List<Category> clist = service1.findCategoryListByName("child");
            request.setAttribute("flist",flist);
            request.setAttribute("clist",clist);

            AddressService service = new AddressServiceImpl();
            List<Address> addresses = service.findAddressByUserId(user.getUser_id());
            request.setAttribute("address",addresses);

            //计算商品价格
            int pid = cart.getProduct_id();
            int count = cart.getProduct_quantity();
            productCount += count;//订单的商品总数量

            ProductService service2 = new ProductServiceImpl();
            Product p = service2.findProductById(Integer.valueOf(pid));
            int price = p.getProduct_price();
            int totalPrice = price*Integer.valueOf(count);
            totalAmount += totalPrice;
            request.setAttribute("price",price);
            request.setAttribute("count",count);
            request.setAttribute("product",p);
            request.setAttribute("totalPrice",totalPrice);

            //查询购物车数量
            String cartCount = "0";
            CartService service3 = new CartServiceImpl();
            if (user != null) {
                cartCount = String.valueOf(service3.findCartCountByUserId(user.getUser_id()));
            }else {
                cartCount = "?";
            }
            request.setAttribute("cartCount",cartCount);

            //添加此订单为待支付订单

            //添加此订单的订单列表
            //查询该订单中商品及数量
            int product_quantity = Integer.valueOf(count);
            int product_id = p.getProduct_id();

            Order order = new Order(System.currentTimeMillis() +""+ user.getUser_id(),user.getUser_id(),totalPrice,new Date(),"0","0","0",product_id,product_quantity);
            ProductService service5 = new ProductServiceImpl();
            Product product = service5.findProductById(product_id);
            order.setProduct_name(product.getProduct_name());
            order.setProduct_photo(product.getProduct_photo());
            order.setProduct_price(product.getProduct_price());

            request.setAttribute("order",order);
            OrderService service4 = new OrderServiceImpl();
            service4.addOrder(order);
            orderList.add(order);
            //删除购物车中该商品的信息
            service3.delCartById(Integer.valueOf(cartId));
        }
        request.setAttribute("shoplist", orderList);
        request.setAttribute("totalAmount", totalAmount);
        request.setAttribute("productCount", productCount);
        request.getRequestDispatcher("orderlist.jsp").forward(request, response);
//        response.sendRedirect(request.getContextPath() + "/showOrder");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

}
